package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.KLineData;
import cn.net.shuxing.yifu.common.bean.UserDealHistory;
import cn.net.shuxing.yifu.common.constant.DealScene;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.DealRecordDto;
import cn.net.shuxing.yifu.common.dto.KLineDto;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/kline")
public class KLineController {


    @Autowired
    private KLineService kLineService;

    @Autowired
    private KLineDataService kLineDataService;

    @Autowired
    private HangBuyService hangBuyService;

    @Autowired
    private HangSaleService hangSaleService;

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private JedisUtil jedisUtil;
//
//    /** 新增 **/
//    /**
//     * 获取K线图数据
//     *
//     * @param minuteCount 按多少分钟分时,单位固定为分,只能是整数,前端自己换算下单位
//     * @param pageNum     页数
//     * @return
//     */
//    @CrossOrigin
//    @RequestMapping("/minute")
//    public BaseDto getKLineData(Integer minuteCount, Integer pageNum) {
//        LocalDateTime startTime = LocalDateTime.now().plusMinutes(0 - (minuteCount * 50 * pageNum));
//        LocalDateTime endTime = startTime.plusMinutes(minuteCount * 50);
//        List<Map<String, Object>> defaultKLineData = kLineService.getDefaultKLineData(startTime, endTime, minuteCount);
//        return BaseDto.ok(defaulFtKLineData);
//    }

    //
    @RequestMapping("/dataOne")
    public BaseDto getKLineDataOne(Integer coinTypeId) {
        KLineDto kLineDataOne = kLineDataService.getKLineDataOne(coinTypeId);
        return BaseDto.ok(kLineDataOne);
    }

    @RequestMapping("/dataTwo")
    public BaseDto getKLineDataByMinute(Integer minuteCount, Integer coinTypeId, Integer pageNum) {
        List<KLineData> kLineData = kLineDataService.getKLineDataTwo(minuteCount, coinTypeId, pageNum);
        return BaseDto.ok(kLineData);
    }

    @RequestMapping("/dataThree")
    public BaseDto getKlineDataThree(Integer coinTypeId) {
        List<DealRecordDto> hangBuy = hangBuyService.getByCoinTypeIdAndState(coinTypeId);
        List<DealRecordDto> hangSale = hangSaleService.getByCoinTypeIdAndState(coinTypeId);
        Map map = new HashMap();
        map.put("hangBuy", hangBuy);
        map.put("hangSale", hangSale);
        return BaseDto.ok(map);
    }

    @RequestMapping("/dataFour")
    public BaseDto getKlineDataFour(Integer coinTypeId, String time, long minuteCount) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String decode = URLDecoder.decode(time);
        LocalDateTime parse = LocalDateTime.parse(decode, df);

        List<UserDealHistory> kLineDealData = userDealHistoryService.getKLineDealData(coinTypeId, parse, minuteCount);
        return BaseDto.ok(kLineDealData);
    }


    /** 新增 **/
    /**
     * 获取K线图月线数据
     * 如果指标是月线,调这个api
     *
     * @param pageNum 页数
     * @return
     */
    @RequestMapping("/month")
    public BaseDto getKLineDataByMonth(Integer pageNum) {
        int monthCount = 1;
//        LocalDateTime startTime = LocalDateTime.now().plusMonths(0 - (monthCount * 50 * pageNum));
//        LocalDateTime endTime = startTime.plusMonths(monthCount * 50);
        LocalDateTime startTime = LocalDateTime.now().plusMonths(0 - (monthCount * 50 * pageNum));
        LocalDateTime endTime = LocalDateTime.now();

        List<Map<String, Object>> defaultKLineData = kLineService.getDefaultKLineDataByMonth(startTime, endTime, monthCount);
        return BaseDto.ok(defaultKLineData);
    }

    /** 新增 **/
    /**
     * 获取深度图的数据
     *
     * @return
     */
    @RequestMapping("/deep")
    public BaseDto getDeepKLineData() {

        List<Map<String, Object>> deepKLineBuyData = kLineService.getDeepKLineBuyData();
        List<Map<String, Object>> deepKLineSaleData = kLineService.getDeepKLineSaleData();

        Map<String, Object> map = new HashMap<>();
        map.put("buyData", deepKLineBuyData);
        map.put("saleData", deepKLineSaleData);

        return BaseDto.ok(map);
        /*
        {
            buyData:[
            {
                singlePrice: 单价,
                dealCount: 交易量
            }
            ],
            saleData:[
            {
                singlePrice: 单价,
                dealCount: 交易量
            }
            ]
        }
         */
    }

    /** 新增 **/
    /**
     * 获取深度成交图的数据
     *
     * @return
     */
    @RequestMapping("/trade")
    public BaseDto getDeepKLineDataByTrade() {

        List<Map<String, Object>> deepKLineBuyData = kLineService.getDeepTradeKLineData(DealScene.HANG_BUY);
        List<Map<String, Object>> deepKLineSaleData = kLineService.getDeepTradeKLineData(DealScene.HANG_SALE);

        Map<String, Object> map = new HashMap<>();
        map.put("buyData", deepKLineBuyData);
        map.put("saleData", deepKLineSaleData);

        return BaseDto.ok(map);
    }

    @Autowired
    private OpenPriceHistoryService openPriceHistoryService;

    @RequestMapping("/kLineDefault")
    public void kLineDefault() {
        String l = "1559472026535";
        jedisUtil.set(RedisKeyCreator.kLineEndTimeMonthOne(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeMinutesOne(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeMinutesFive(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeMinutesfifteen(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeMinutesThirty(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeDayOne(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeDaySeven(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeHourOne(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeHourTwo(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeHourFour(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.kLineEndTimeHourEight(), String.valueOf(l));
        jedisUtil.set(RedisKeyCreator.openPrice(1),openPriceHistoryService.getByCoinTypeId(1).toString());
        jedisUtil.set(RedisKeyCreator.todayDealCount(1),"0");
        jedisUtil.set(RedisKeyCreator.coinHighestPrice(1),"0");
        jedisUtil.set(RedisKeyCreator.coinLowestPrice(1),"0");
    }

    @RequestMapping("/calculate")
    public void caculate(Integer minuteCount){
        kLineDataService.calculateKLineDataByMinuteUnit(minuteCount);
    }
}
